package com.yuncheng.oaApi.FlowController;

import com.yuncheng.oaApi.vo.OaFlowVo;
import com.yuncheng.oaApi.vo.OaReturnVo;
import com.yuncheng.oaApi.vo.OaXcxxspglVo;
import com.yuncheng.oaApi.vo.Result;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.oaApi.FlowService.NewOaXcxxspglOaFlowService;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.oaApi.busService.OaDbrService;
import com.yuncheng.oaApi.busService.OaFlowNodeDbrService;
import com.yuncheng.oaApi.busService.MyLcFlowService;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaLogConstant;
import com.yuncheng.oaApi.constant.OaFlowConstant;
import com.yuncheng.oaApi.constant.OaXcxxspOaFlowConstants;
import com.yuncheng.oaApi.controller.OaXzswspFlowController;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.oaApi.mapper.OaXcxxspglFlowMapper;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Api(tags = "NEW_OA-宣传信息审批")
@RestController
@RequestMapping("/api/spzx-oa/newflow-xcxxsp")
public class NewOaXcxxspglFlowController {
    private static final Logger log = LoggerFactory.getLogger(OaXzswspFlowController.class);


    @Resource
    private OaXcxxspglFlowMapper oaXcxxspglFlowMapper;

    @Resource
    private NewOaXcxxspglOaFlowService newOaXcxxspglFlowService;

    @Resource
    private IOaXcxxspglService oaXcxxspglServicel;

    @Resource
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    @Lazy
    private IOaSysTzMessageService sysTzMessageService;

    @Resource
    @Lazy
    private OaDbrService oaDbrService;

    @Resource
    @Lazy
    private MyLcFlowService myLcFlowService;

    @Resource
    @Lazy
    private IOaSysFlowTempleService sysFlowTempleService;

    @Resource
    @Lazy
    private IOaSysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private OaFlowNodeDbrService oaFlowNodeDbrService;

    @Resource
    @Lazy
    private IOaProcessBusinessService oaProcessBusinessService;


    /**
     * 流程列表分页查询
     *
     * @param oaXcxxspglVo
     * @param pageNo
     * @param pageSize
     * @param onlyCurrentUser
     * @return
     */
    @AutoLog(value = "流程监控-分页查询")
    @ApiOperation(value = "流程监控-分页查询")
    @GetMapping("flowJkPage")
    public HttpResult<?> flowJkPage(
            OaXcxxspglVo oaXcxxspglVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "false") boolean onlyCurrentUser) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<OaXcxxspglVo> page = new Page<>(pageNo, pageSize);
        oaXcxxspglVo.setFlcmc(OaFlowConstant.OA_XCXXSP_NAME);

        IPage<OaXcxxspglVo> list = null;
        if (StringUtils.isNotBlank(oaXcxxspglVo.getOaid())){
            OaXcxxspgl xcxxspgl = oaXcxxspglServicel.getById(oaXcxxspglVo.getOaid());
            if (xcxxspgl != null){
                list = oaXcxxspglFlowMapper.flowJkPage(page, onlyCurrentUser, user, oaXcxxspglVo);
            }
        }



        return HttpResult.ok(list);
    }

    /**
     * 流程列表分页查询
     *
     * @param oaXcxxspglVo
     * @param pageNo
     * @param pageSize
     * @param onlyCurrentUser
     * @return
     */
    @AutoLog(value = "流程列表-分页查询")
    @ApiOperation(value = "流程列表-分页查询")
    @GetMapping("flowJobPage")
    public HttpResult<?> flowJobPage(
            OaXcxxspglVo oaXcxxspglVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        if (StringUtils.isBlank(oaXcxxspglVo.getFblzt())) {
            oaXcxxspglVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        }
        Page<OaXcxxspglVo> page = new Page<>(pageNo, pageSize);
        oaXcxxspglVo.setFlcmc(OaFlowConstant.OA_XCXXSP_NAME);
        IPage<OaXcxxspglVo> list = oaXcxxspglFlowMapper.flowJobPage(page, onlyCurrentUser, user, oaXcxxspglVo);
        return HttpResult.ok(list);
    }



    /**
     * 我的已办
     */
    @AutoLog(value = "流程列表-我的已办")
    @ApiOperation(value = "流程列表-我的已办")
    @GetMapping("myJobPage")
    public HttpResult<?> myJobPage(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            OaXcxxspglVo oaXcxxspglVo) {

        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<OaXcxxspglVo> page = new Page<>(pageNo, pageSize);
        IPage<OaXcxxspglVo> list = oaXcxxspglFlowMapper.myYbFlowPage(page, true, user, oaXcxxspglVo);
        return HttpResult.ok(list);
    }

    /**
     * 数据修订
     */
    @AutoLog(value = "列表-数据修订")
    @ApiOperation(value = "列表-数据修订")
    @GetMapping("Pagesjxd")
    public HttpResult<?> Pagesjxd(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            OaXcxxspglVo oaXcxxspglVo) {

        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }

        Page<OaXcxxspglVo> page = new Page<>(pageNo, pageSize);
        IPage<OaXcxxspglVo> list = oaXcxxspglFlowMapper.myYbFlowPage(page, false, user, oaXcxxspglVo);
        return HttpResult.ok(list);
    }


    @AutoLog(value = "发起宣传信息审批流程")
    @ApiOperation(value = "发起宣传信息审批流程")
    @PostMapping(value = "initBzFlow")
    public HttpResult<?> initBzFlow(@RequestBody OaXcxxspgl oaXcxxspgl) {
        String oaid = "";
        //当前人的id
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (currentUser == null) {
            return HttpResult.error("当前登录已失效，请重新登录");
        }
        if (StringUtils.isNotBlank(oaXcxxspgl.getId())) {
            OaXcxxspgl oaids = oaXcxxspglServicel.getById(oaXcxxspgl.getId());
            ConvertUtils.register(new DateConvert(), Date.class);
            try {
                BeanUtils.populate(oaids, BeanUtils.describe(oaXcxxspgl));
            } catch (IllegalAccessException e) {
                log.error("出错-保存事项【非法访问异常】,{}", e.getMessage());
            } catch (InvocationTargetException e) {
                log.error("出错-保存事项【调用TargetException】,{}", e.getMessage());
            } catch (NoSuchMethodException e) {
                log.error("出错-找不到可用方法", e.getMessage());
            }
            oaid = oaXcxxspgl.getId();
            oaids.setFuserid(currentUser.getId());
            oaXcxxspglServicel.updateById(oaids);
        } else {
            oaid = GetUuIdUtils.ReplaceUuId();
            oaXcxxspgl.setId(oaid);
            oaXcxxspgl.setFngr(currentUser.getName());
            oaXcxxspgl.setFuserid(currentUser.getId());
            oaXcxxspgl.setFngks(currentUser.getDeptName());
            oaXcxxspgl.setFlczt(OaCommonFlowConstant.FLOW_LCZB);
            oaXcxxspglServicel.save(oaXcxxspgl);
        }
        /**
         * @Author: lhb
         * @Description : 是否发起流程   如果是开始申请就跳转流程
         *               在这里设置 nextNodeName 指定下个环节节点 做当前跳转流程的判断，然后设置变量接收环节的值，写到nextNodeName中
         * @DateTime:
         * @Params: Fsffqlc
         */
        String msg = "保存成功";
        String lcmbCode = OaFlowConstant.OA_XCXXSP;
        OaJobEntity job = new OaJobEntity();
        // 初始化流程
        Result result = newOaXcxxspglFlowService.createProcess(lcmbCode,oaid, currentUser);
        //做一次规则校验 如果查到此oaid 在表中已经插入 那就执行更新
        OaXcxxspgl oaXcxxspgl1 = oaXcxxspglServicel.getById(oaid);
        List<OaProcessBusiness> oaProcessBusinessList = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                .eq(OaProcessBusiness::getFpid, oaid));
        String bt =  currentUser.getName()+"提交的宣传信息审批"; //标题
        String zy = "拟稿科室:["+oaXcxxspgl1.getFngks()+"]"+"\n"+
                "信息名称:["+ oaXcxxspgl1.getFxxmc()+"]"+"\n"
                +"发布栏目:["+ oaXcxxspgl1.getFfblm() + "]"+"\n";
        if (oaProcessBusinessList.size() == 0){
            //初始化的时候 像 流程业务主表添加拼接初始化数据
            oaProcessBusinessService.addProcess(oaid,zy,bt,lcmbCode,OaFlowConstant.OA);

        }else {
            String id = oaProcessBusinessList.get(0).getId();  //当前已经插入的主键id 然后更新 摘要信息
            OaProcessBusiness oaProcessBusiness = oaProcessBusinessService.getById(id);
            oaProcessBusiness.setFzy(zy);
            oaProcessBusinessService.updateById(oaProcessBusiness);

        }
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }else {
             job = (OaJobEntity)result.getResult();
            if (job != null) {
                // 是否提交流程
                if(StringUtils.isBlank(oaid)){
                    return HttpResult.error("参数丢失");
                }
                String nextNodeId = "";
                if (StringUtils.isNotBlank(oaXcxxspgl.getFsffqlc()) && oaXcxxspgl.getFsffqlc().equals(OaCommonFlowConstant.SFFQLC_YES)) {
                    //发起流程的时候判断当前人的部门和职称 职称为科长的时候 跳过科长审批 部门为中心副主任的时候直接到中心主任
                    String orgName = currentUser.getOrgName();


                    if (StringUtils.isBlank(orgName)) {
                        return HttpResult.error("请完善个人信息部门名称栏");
                    }
                    List<OaSysFlowTemple> oaSysFlowTemples = sysFlowTempleService.list(new LambdaQueryWrapper<OaSysFlowTemple>().eq(OaSysFlowTemple::getFlcbm, lcmbCode));
                    String templeId = oaSysFlowTemples.get(0).getId();
                    //
                    List<OaSysFlowNode> oaSysFlowNodesDwldsp = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getFlcmbid, templeId)
                            .eq(OaSysFlowNode::getFjdmc,OaFlowConstant.XCXX_HJMC_DWLDSP));

                    List<OaSysFlowNode> oaSysFlowNodesBgssp = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getFlcmbid, templeId)
                            .eq(OaSysFlowNode::getFjdmc,OaFlowConstant.XCXX_HJMC_BGSSSP));




                    if (orgName.contains(OaCommonFlowConstant.ORG_NAME_ZXFZR)) {
                        nextNodeId = oaSysFlowNodesDwldsp.get(0).getId();
                    } else if (oaFlowNodeDbrService.brksfzr()) {
                        nextNodeId = oaSysFlowNodesBgssp.get(0).getId();
                    } else if (orgName.equals(OaCommonFlowConstant.BGS_SP_FLOW)){
                        nextNodeId = oaSysFlowNodesBgssp.get(0).getId();
                    }else {
                        nextNodeId = "";
                    }

                    Result result1 = newOaXcxxspglFlowService.passJob(oaid, job.getId(), "同意", nextNodeId, "");
                    if (!result1.isSuccess()) {
                        return HttpResult.error(result.getMessage());
                    }
                    //提交完成后 更新待办信息
                    sysTzMessageService.tjMessage(oaid);
                    List<OaProcessBusiness> oaProcessBusinessList1 = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                            .eq(OaProcessBusiness::getFpid, oaid));
                    OaProcessBusiness oaProcessBusiness = oaProcessBusinessList1.get(0);
                    OaProcessBusiness byId1 = oaProcessBusinessService.getById(oaProcessBusiness.getId());
                    byId1.setFsfkch(0);
                    byId1.setFcshdb(OaFlowConstant.FLOW_BLZT_YB);
                    oaProcessBusinessService.updateById(byId1);
                    myLcFlowService.recall(oaid);
                    msg = "提交成功";
                }
            }
        }
//        HttpResult http = new HttpResult(true);
//        http.setMessage(msg);
//        http.setResult(oaXcxxspgl);
        HashMap map = new HashMap();
        map.put("msg",msg);
        map.put("oaMap",oaXcxxspgl);
        map.put("jobMap",job);
        return HttpResult.ok(map);
    }


    /**
     * 宣传信息审批流程保存接口
     */
    @AutoLog(value = "宣传信息审批流程保存接口")
    @ApiOperation(value = "宣传信息审批流程保存接口")
    @PostMapping(value = "/saveFlow")
    public HttpResult<?> saveFlow(@RequestBody OaFlowVo oaFlowVo) {
        String submitType = OaCommonFlowConstant.SUBMIT_TYPE_SAVE; // 保存

        String jobId = oaFlowVo.getJobid(); // 当前流程任务id
        String oaid = oaFlowVo.getOaid(); // 当前办公主键id
        String opinion = oaFlowVo.getOpinion(); // 办理意见

        if (StringUtils.isBlank(jobId)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }
        // 流程处理
        Result httpResult = newOaXcxxspglFlowService.handleFlow(oaFlowVo, null, jobId, oaid, submitType, opinion);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }
        Map map = new HashMap();
        HttpResult<Object> res = new HttpResult<>(true);
        res.setMessage("保存成功");
        res.setResult(map);
        return res;
    }

    /**
     * 宣传信息审批流程提交接口
     */
    @AutoLog(value = "宣传信息审批流程提交接口")
    @ApiOperation(value = "宣传信息审批流程提交接口")
    @PostMapping(value = "/submitFlow")
    public HttpResult<?> submitFlow(@RequestBody OaFlowVo oaFlowVo) {
        String submitType = OaCommonFlowConstant.SUBMIT_TYPE_SUBMIT; // 提交

        String jobId = oaFlowVo.getJobid();
        String oaid = oaFlowVo.getOaid();
        String opinion = oaFlowVo.getOpinion();
        OaXcxxspgl oaId = oaXcxxspglServicel.getById(oaid);
        OaJobEntity jobid = spcySqsxJobService.getById(jobId);

        if (jobid.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_XXFBBL)) {

        } else {
            oaId.setFlczt(OaCommonFlowConstant.FLOW_LCZB);
        }
        if (StringUtils.isBlank(oaId.getFsffqlc()) || oaId.getFsffqlc().equals(OaCommonFlowConstant.SFFQLC_NO)) {
            oaId.setFsffqlc(OaCommonFlowConstant.SFFQLC_YES);
        }
        oaXcxxspglServicel.updateById(oaId);

        if (StringUtils.isBlank(jobId)) {
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }
        OaJobEntity nowJob = null;
        Result httpResult1 = newOaXcxxspglFlowService.handleFlow(oaFlowVo, nowJob, jobId, oaid, submitType, opinion);
        if (!httpResult1.isSuccess()){
            return HttpResult.error(httpResult1.getMessage());
        }
        List<OaProcessBusiness> oaProcessBusinessList1 = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                .eq(OaProcessBusiness::getFpid, oaid));
        OaProcessBusiness oaProcessBusiness = oaProcessBusinessList1.get(0);
        OaProcessBusiness byId1 = oaProcessBusinessService.getById(oaProcessBusiness.getId());
        byId1.setFsfkch(0);
        byId1.setFcshdb(OaFlowConstant.FLOW_BLZT_YB);
        oaProcessBusinessService.updateById(byId1);
        //提交完成后 更新待办信息
        sysTzMessageService.tjMessage(oaid);
        myLcFlowService.recall(oaid);
        return HttpResult.ok("提交成功");
    }

    /**
     * 查询当前流程任务详情-通过当前流程任务id
     */
    @AutoLog(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiImplicitParam(value = "jobid", name = "流程任务id", required = true)
    @GetMapping(value = "queryJobByJobId")
    public HttpResult<?> queryJobByJobId(@RequestParam("jobid") String jobid) {
        if (StringUtils.isBlank(jobid)) {
            return HttpResult.error("流程任务id不能为空");
        }
        Map map = newOaXcxxspglFlowService.getJobConcernByJobId(jobid);
        return HttpResult.ok(map);
    }

    /**
     * 获取已办列表-通过当前流程id-自定义退回
     *
     * @param jobid
     * @return
     */
    @AutoLog(value = "获取已办列表-通过当前流程id-自定义退回")
    @ApiOperation(value = "获取已办列表-通过当前流程id-自定义退回")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobid", value = "流程任务id"),
            @ApiImplicitParam(name = "isZdhj", value = "是否指定环节，默认：true，指定")
    })
    @GetMapping(value = "queryAllYbJob")
    public HttpResult<?> queryAllYbJob(@RequestParam(value = "jobid", defaultValue = "") String jobid,
                                       @RequestParam(value = "isZdhj", defaultValue = "true") boolean isZdhj) {

        List<Map<String, String>> currentJobPrevious = newOaXcxxspglFlowService.getCurrentJobPrevious(jobid, isZdhj);
        return HttpResult.ok(currentJobPrevious);
    }

    /**
     * 退回环节
     *
     * @return
     */
    @AutoLog(value = "自定义-退回环节")
    @ApiOperation(value = "自定义-退回环节")
    @PostMapping(value = "returnUpperzdyFlowJob")
    public HttpResult<?> returnUpperzdyFlowJob(@RequestBody OaReturnVo oaReturnVo) {
        newOaXcxxspglFlowService.returnZdJob(oaReturnVo);
        return HttpResult.ok("退回成功");
    }






    @AutoLog(value = "删除宣传信息审批管理流程")
    @ApiOperation(value = "删除宣传信息审批管理流程")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobids", value = "流程任务id,逗号分割", required = true)
    })
    @DeleteMapping(value = "deleteLgqsFlowJob")
    public HttpResult<?> deleteLgqsFlowJob(@RequestParam(value = "jobids") String jobids) {
        if (StringUtils.isBlank(jobids)) {
            return HttpResult.error("参数不能为空");
        }
        List<String> jobIdList = Arrays.asList(jobids.split(","));
        String logNr = "";
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(jobIdList)){
            List<OaJobEntity> jobList = spcySqsxJobService.listByIds(jobIdList);
            if (CollectionUtils.isNotEmpty(jobList)){
                for (OaJobEntity job : jobList) {
                    spcySqsxJobService.removeById(job.getId());
                    OaXcxxspgl oaXcxxspgl = oaXcxxspglServicel.getById(job.getFpid());
                    if (oaXcxxspgl != null) {
                        oaXcxxspglServicel.removeById(job.getFpid());

                        logNr = "删除宣传信息审批流程" + job.getFhjmc() + ": ";
                        map = ObjectToMapUtils.objectToMap(oaXcxxspgl);
                        logNr += LogUtils.MapToStringMethod(map);

                        spcyLogService.addLogs(job.getFhjmc(), logNr, job.getFpid(), OaLogConstant.LOGS_OA_XCXXSP, OaCommonFlowConstant.LOGS_SYS_TYPE_SC);
                    }
                }
            }
        }

        return HttpResult.ok("删除成功");
    }


    @AutoLog(value = "退回指定环节")
    @ApiOperation(value = "退回指定环节")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "jobid", name = "当前流程任务jobid", required = true),
            @ApiImplicitParam(value = "opinion", name = "退回意见", required = true),
            @ApiImplicitParam(value = "shjid", name = "选择退回的环节id", required = true)
    })
    @PostMapping(value = "returnUpperFlowJobs")
    public HttpResult<?> returnUpperFlowJobs(@RequestBody Map<String, String> map) {
        String jobid = map.get("jobid");
        String opinion = map.get("opinion");
        String shjid = map.get("shjid");

        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (currentUser == null) {
            return HttpResult.error("登录已失效，请重新登录");
        }
        OaJobEntity job = spcySqsxJobService.getById(jobid);
        // 待办-获取上一个环节id
        OaJobEntity syhjJob = spcySqsxJobService.getById(shjid);
        if (job == null) {
            return HttpResult.error("当前流程任务不存在");
        }
        if (syhjJob == null) {
            return HttpResult.error("当前选择退回的环节不存在");
        }
        // 办理状态 - 待办
        if (StringUtils.isNotBlank(job.getFblzt()) && (job.getFblzt().equals(OaCommonFlowConstant.BLZT_DB)
                || job.getFblzt().equals(OaCommonFlowConstant.BLZT_TH))) {
            String oldshjid = job.getFsyhjid(); // 当前流程上环节

            // 如果 当前 流程上环节与 选择 退回的环节 相等 则删除.否则不等，则修改办理状态
            if (!oldshjid.equals(shjid)) {
                String del = dealDelNodeJob(shjid);
            }
            if (syhjJob.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_SQ)) {
                OaXcxxspgl sqsx = oaXcxxspglServicel.getById(job.getFpid());
                if (sqsx != null) {
                    oaXcxxspglServicel.updateById(sqsx);
                }
            }
            syhjJob.setFthyj(opinion);
            syhjJob.setFfssj(new Date());
            syhjJob.setFblzt(OaCommonFlowConstant.BLZT_TH);
            syhjJob.setFfsrid(currentUser.getId());
            syhjJob.setFfsrmc(currentUser.getName());
            spcySqsxJobService.updateById(syhjJob);

            spcySqsxJobService.removeById(jobid); // 删除当前流程任务待办
        }

        String logNr = "退回上一环节: 从[" + job.getFhjmc() + "]退回到[" + syhjJob.getFhjmc() + "];";
        Map<String, Object> logMap = ObjectToMapUtils.objectToMap(job);
        logNr += LogUtils.MapToStringMethod(logMap);
        spcyLogService.addLogs(job.getFhjmc(), logNr, job.getFpid(), OaLogConstant.LOGS_SQSX_JOB, OaCommonFlowConstant.LOGS_SYS_TYPE_TH);

        return HttpResult.ok("退回成功");
    }

    /**
     * 递归删除当前流程任务父级的所有流程任务（）
     */
    public String dealDelNodeJob(String shjid) {
        String id = "";
        List<OaJobEntity> shjList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getFsyhjid, shjid));
        if (CollectionUtils.isNotEmpty(shjList)) {
            for (OaJobEntity job : shjList) {
                id = job.getId();
                if (job.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_SQ)) {
                    OaXcxxspgl sqsx = oaXcxxspglServicel.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaXcxxspglServicel.updateById(sqsx);
                    }
                }

                if (job.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_KSSP)) {
                    OaXcxxspgl sqsx = oaXcxxspglServicel.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaXcxxspglServicel.updateById(sqsx);
                    }
                }

                if (job.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_BGSSSP)) {
                    OaXcxxspgl sqsx = oaXcxxspglServicel.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaXcxxspglServicel.updateById(sqsx);
                    }
                }

                if (job.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_FGLDSP)) {
                    OaXcxxspgl sqsx = oaXcxxspglServicel.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaXcxxspglServicel.updateById(sqsx);
                    }
                }
                if (job.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_DWLDSP)) {
                    OaXcxxspgl sqsx = oaXcxxspglServicel.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaXcxxspglServicel.updateById(sqsx);
                    }
                }
                if (job.getFhjmc().equals(OaXcxxspOaFlowConstants.HJMC_XXFBBL)) {
                    OaXcxxspgl sqsx = oaXcxxspglServicel.getById(shjList.get(0).getFpid());
                    if (sqsx != null) {
                        oaXcxxspglServicel.updateById(sqsx);
                    }
                }

                spcySqsxJobService.removeById(id);
                return dealDelNodeJob(id);
            }
        }
        return id;
    }

}
